﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Common;
using System.Web.SessionState;
using System.Dynamic;
using Model;
using DAL;
using System.Data;
using System.Dynamic;
using System.Net;
using System.Text;
using System.Web.SessionState;
using JPushAPI;
using System.Security.Cryptography;
using WxPayAPI;
using CsharpHttpHelper;
namespace UI.tools
{
    /// <summary>
    /// Admin 的摘要说明
    /// </summary>
    public class Admin : IHttpHandler, IRequiresSessionState
    {
        static Dictionary<string, Func<string>> hs = new Dictionary<string, Func<string>>();
        static int count = 1;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = JobRequest.GetRequestString("act");
            string result = string.Empty;
            if (Commons.CheckSign())
            {
                result = hs[action]();
            }
            else
            {
                result = Commons.GetFaild("身份验证失败");
            }
            context.Response.Write(result);
            context.Response.End();
        }
        static Admin()
        {
            /*后台登陆*/
            #region 获取管理员列表
            hs.Add("Find_Admin", () =>
            {
                //参数
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;

                List<AdminInfo> list = AdminDAL.FindPage(keyword, begindate, enddate, pageindex, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 保存管理员
            hs.Add("Save_Admin", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                string username = JobRequest.GetRequestString("username"); //用户名
                string realname = JobRequest.GetRequestString("realname"); //真是姓名
                string userpwd = DESEncrypt.Encrypt(JobRequest.GetRequestString("userpwd")); //密码
                int role = JobRequest.GetRequestInt("role"); //角色
                string power = JobRequest.GetRequestString("power"); //权限
                int usercount = JobRequest.GetRequestInt("usercount"); //会员数量
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                AdminInfo info = new AdminInfo()
                {
                    ID = id,
                    ParentID = B.MyAdmin.ID,
                    UserName = username,
                    UserPwd = userpwd,
                    RealName = realname,
                    Power = power,
                    Role = role,
                    UserCount = usercount,
                    CreateDate = DateTime.Now,
                };
                if (id == 0)
                {
                    ID = AdminDAL.Save(info);
                }
                else
                {
                    ID = AdminDAL.Update(info);
                }
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 后台登陆
            hs.Add("Login", () =>
            {
                //参数
                try
                {
                    string username = JobRequest.GetRequestString("username"); //用户名
                    string userpwd = JobRequest.GetRequestString("userpwd"); //密码
                    int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                    Commons.Log2((count++) + "--" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
                    //参数
                    AdminInfo info = AdminDAL.Login(username, DESEncrypt.Encrypt(userpwd));
                    if (info.ID > 0)
                    {
                        if (source == 1 || source == 0)
                        {
                            B.MyAdmin = info;
                        }
                        return Commons.GetSuccess("登陆成功", info);
                    }
                    else
                    {
                        return Commons.GetFaild("用户名或密码错误");
                    }
                }
                catch (Exception ex)
                {
                    Commons.Log(ex.Message);
                    return Commons.GetFaild(ex.Message);
                }
            });
            #endregion
            #region 修改密码
            hs.Add("Update_Pwd", () =>
            {
                //参数
                int userid = JobRequest.GetRequestInt("userid"); //用户ID
                string oldpwd = JobRequest.GetRequestString("oldpwd"); //旧密码
                string newpwd = JobRequest.GetRequestString("newpwd"); //新密码
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                AdminInfo user = new AdminInfo();
                if (source == 1 || source == 0)
                {
                    user = B.MyAdmin;
                }
                else
                {
                    user = AdminDAL.FindOneByID(userid);
                }

                if (user.UserPwd != oldpwd)
                {
                    return Commons.GetFaild("旧密码错误");
                }
                bool ok = CommonDAL.UpdateColumnByID(user.ID, "userpwd", DESEncrypt.Encrypt(newpwd), "[admin]");
                if (ok)
                {
                    if (source == 1 || source == 0)
                    {
                        B.MyAdmin.UserPwd = newpwd;
                    }
                    return Commons.GetSuccess("修改成功", "");
                }
                else
                {
                    return Commons.GetFaild("修改失败");
                }
            });
            #endregion
            #region 删除管理员
            hs.Add("Delete_Admin", () =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.DeleteByIDs(ids, "[admin]");
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 商家登陆
            hs.Add("UserLogin", () =>
            {
                //参数
                try
                {
                    string username = JobRequest.GetRequestString("username"); //用户名
                    string userpwd = JobRequest.GetRequestString("userpwd"); //密码
                    int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                    //参数
                    UserInfo info = UserDAL.Login(username, DESEncrypt.Encrypt(userpwd));
                    if (info.ID > 0)
                    {
                        if (source == 1 || source == 0)
                        {
                            B.MyUser = info;
                        }
                        return Commons.GetSuccess("登陆成功", info);
                    }
                    else
                    {
                        return Commons.GetFaild("用户名或密码错误");
                    }
                }
                catch (Exception ex)
                {
                    return Commons.GetFaild(ex.Message);
                }
            });
            #endregion

            /*用户*/
            #region 获取用户列表
            hs.Add("Find_User", () =>
            {
                //参数
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int state = JobRequest.GetRequestInt("state"); //状态
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<UserInfo> list = UserDAL.FindPage(keyword, state, begindate, enddate, pageindex, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 设置用户级别
            hs.Add("RoleSet_User", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int role = JobRequest.GetRequestInt("role"); //级别
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                UserInfo user = UserDAL.FindOneByID(id);
                AdminInfo admin = B.MyAdmin;
                if (user.AdminID != 0 && user.AdminID != admin.ID)
                {
                    return Commons.GetFaild("您无权操作该会员");
                }
                if (UserDAL.FindCountByAdminID(admin.ID) >= admin.UserCount && role == 2)
                {
                    return Commons.GetFaild("您可以开通的最大会员数量为" + admin.UserCount);
                }
                bool ok = CommonDAL.UpdateColumnByID(id, "Role", role, "[user]");
                if (ok)
                {
                    CommonDAL.UpdateColumnByID(id, "AdminID", admin.ID, "[user]");
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 分配代理商
            hs.Add("AdminSet_User", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int adminid = JobRequest.GetRequestInt("adminid"); //代理商ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "AdminID", adminid, "[user]");
                if (ok)
                {
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 审核用户
            hs.Add("Audit_User", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int state = JobRequest.GetRequestInt("state"); //状态
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "State", state, "[user]");
                if (ok)
                {
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 删除用户
            hs.Add("Delete_User", () =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.DeleteByIDs(ids, "[user]");
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 修改用户信息
            hs.Add("Save_User", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                string mobile = JobRequest.GetRequestString("mobile"); //手机号
                string userpwd = DESEncrypt.Encrypt(JobRequest.GetRequestString("userpwd")); //密码
                DateTime enddate = DataConvert.ToDateTime(JobRequest.GetRequestString("enddate"));
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                UserInfo info = new UserInfo()
                {
                    ID = id,
                    Mobile = mobile,
                    UserPwd = userpwd,
                    EndDate = enddate
                };
                if (id > 0)
                {
                    if (B.MyAdmin.Role == 1)
                    {
                        info.State = 1;
                    }
                    else
                    {
                        info.State = 0;
                    }
                    ID = UserDAL.UpdateUser(info);
                }
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 批量分配代理商
            hs.Add("Batch_AdminSet_User", () =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //ID
                int adminid = JobRequest.GetRequestInt("adminid"); //代理商ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                ids = ids.Trim(',');
                string[] idlist = ids.Split(',');
                for (int i = 0; i < idlist.Length; i++)
                {
                    CommonDAL.UpdateColumnByID(DataConvert.ToInt(idlist[i]), "AdminID", adminid, "[user]");
                }
                return Commons.GetSuccess("操作成功", "");

            });
            #endregion
            #region 获取未审核的数据
            hs.Add("Get_AuditCount", () =>
            {
                int count = UserDAL.GetAuditCount();
                return Commons.GetSuccess("操作成功", count);
            });
            #endregion
            #region 设置关键词
            hs.Add("Set_UserKeyword", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                string keyword = JobRequest.GetRequestString("keyword"); //关键词
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "Keyword", keyword, "[user]");
                if (ok)
                {
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 设置入账详情
            hs.Add("Set_Business", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                string business = JobRequest.GetRequestString("business"); //关键词
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "Business", business, "[user]");
                if (ok)
                {
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 修改余额
            hs.Add("Update_Money", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                float money = JobRequest.GetRequestFloat("money"); //余额
                money = money * 100;
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "Money", money, "[user]");
                if (ok)
                {
                    return Commons.GetSuccess("修改成功", "");
                }
                else
                {
                    return Commons.GetFaild("修改失败");
                }
            });
            #endregion
            #region 修改投放图标
            hs.Add("ShowPutSet_User", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int showput = JobRequest.GetRequestInt("showput"); //是否显示图标
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "ShowPut", showput, "[user]");
                if (ok)
                {
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 同步数据
            hs.Add("Set_Synch", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int userid = JobRequest.GetRequestInt("userid"); //旧用户ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                UserInfo user = UserDAL.FindOneByID(userid);
                bool ok = UserDAL.SetSynch(id, user.State, user.Role, user.Money*100, user.EndDate, user.AdminID, user.Keyword, user.Business, user.ShowPut);
                if (ok)
                {
                    ActiveDAL.SetSynch(userid, id);
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion

            /*活动*/
            #region 保存活动
            hs.Add("Save_Active", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int userid = JobRequest.GetRequestInt("userid"); //用户ID
                string name = JobRequest.GetRequestString("name"); //活动名称
                string img = JobRequest.GetRequestString("img").Replace(Commons.domain, "").Replace("_100.", "."); //图片
                string context = JobRequest.GetRequestString("context"); //公告内容
                string link = JobRequest.GetRequestString("link"); //活动链接
                string mobile = JobRequest.GetRequestString("mobile"); //联系电话
                int prizetype = JobRequest.GetRequestInt("prizetype"); //领奖方式
                int clickcount = JobRequest.GetRequestInt("clickcount"); //点击量
                int sharecount = JobRequest.GetRequestInt("sharecount"); //分享量
                int takecount = JobRequest.GetRequestInt("takecount"); //已领数量
                float takemoney = JobRequest.GetRequestFloat("takemoney") * 100; //已领金额
                int showover = JobRequest.GetRequestInt("showover"); //结束图提醒
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                if ((link.Contains("http://www.r1973.cn") || link.Contains("http://www.dchsksw.com")) && link.Contains("&code="))
                {
                    link = link.Substring(0, link.IndexOf("&code="));
                }
                ActiveInfo info = new ActiveInfo()
                {
                    ID = id,
                    UserID = userid,
                    Name = name,
                    Img = img,
                    Link = link,
                    Mobile = mobile,
                    Context = context,
                    ClickCount = clickcount,
                    ShareCount = sharecount,
                    PrizeType = prizetype,
                    TakeCount = takecount,
                    TakeMoney = takemoney,
                    ShowOver = showover,
                    CreateDate = DateTime.Now
                };
                int ID = 0;
                if (id == 0)
                {
                    ID = ActiveDAL.Save(info);
                }
                else
                {
                    DataRow dr = CommonDAL.GetDataRow(string.Format(@"select ClickCount,ShareCount,UserID from active where ID={0}", id));
                    int OldClickCount = DataConvert.ToInt(dr["ClickCount"]);
                    int OldShareCount = DataConvert.ToInt(dr["ShareCount"]);
                    int createid = DataConvert.ToInt(dr["UserID"]);

                    ID = ActiveDAL.Edit(info);
                    VisitRecordInfo visit = new VisitRecordInfo()
                    {
                        UserID = 0,
                        SID = id,
                        Province = "",
                        City = "",
                        Area = "",
                        Lat = 0,
                        Lng = 0,
                        Address = "",
                        CreateID = createid,
                        FCount = 1,
                        Flag = 1,
                        CreateDate = DateTime.Now
                    };
                    if (clickcount - OldClickCount > 0)
                    {
                        visit.FCount = clickcount - OldClickCount;
                        visit.Flag = 1;
                        VisitRecordDAL.Save(visit);
                    }
                    if (sharecount - OldShareCount > 0)
                    {
                        visit.FCount = sharecount - OldShareCount;
                        visit.Flag = 2;
                        VisitRecordDAL.Save(visit);
                    }

                }
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 保存用户活动
            hs.Add("Save_UserActive", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                string name = JobRequest.GetRequestString("name"); //活动名称
                int moneytype = JobRequest.GetRequestInt("moneytype"); //红包类型
                int count = JobRequest.GetRequestInt("count"); //红包数量
                float onemoney = JobRequest.GetRequestFloat("onemoney") * 100; //单个红包金额
                float allmoney = JobRequest.GetRequestFloat("allmoney") * 100; //红包总金额
                string serverid = JobRequest.GetRequestString("serverid"); //服务id
                string img = JobRequest.GetRequestString("img").Replace(Commons.domain, "").Replace("_100.", "."); //图片
                string context = JobRequest.GetRequestString("context"); //公告内容
                string link = JobRequest.GetRequestString("link"); //活动链接
                string mobile = JobRequest.GetRequestString("mobile"); //联系电话
                int prizetype = JobRequest.GetRequestInt("prizetype"); //领奖方式
                int range = JobRequest.GetRequestInt("range"); //投放范围
                string province = JobRequest.GetRequestString("province"); //省
                string city = JobRequest.GetRequestString("city"); //市
                string area = JobRequest.GetRequestString("area"); //区
                string address = JobRequest.GetRequestString("address"); //详细地址
                float lat = JobRequest.GetRequestFloat("lat"); //纬度
                float lng = JobRequest.GetRequestFloat("lng"); //经度
                int metre = JobRequest.GetRequestInt("metre"); //米
                int degree = JobRequest.GetRequestInt("degree"); //匹配度
                int prizecount = JobRequest.GetRequestInt("prizecount"); //领奖次数
                int showover = JobRequest.GetRequestInt("showover"); //结束图提醒
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                UserInfo user = UserDAL.FindOneByID(B.MyUser.ID);
                int userid = user.ID;
                if (user.Role == 1)
                {
                    //return Commons.GetFaild("您还不是会员");
                }
                if (moneytype == 1 && JobRequest.GetRequestFloat("allmoney") / count < 0.3)
                {
                    return Commons.GetFaild("人均红包金额不能少于0.3元");
                }
                if (moneytype == 2 && JobRequest.GetRequestFloat("onemoney") < 0.3)
                {
                    return Commons.GetFaild("单个红包金额不能少于0.3元");
                }
                if (user.EndDate < DateTime.Now || user.State != 1)
                {
                    if (count < 1400)
                    {
                        return Commons.GetFaild("非会员发送红包数量不能小于1400个");
                    }
                }
                if (id == 0 && user.Money * 100 < allmoney)
                {
                    return Commons.GetFaild("您的余额不足");
                }
                int ID = 0;
                WeiXin wx = new WeiXin();
                string path = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd");
                string root = HttpContext.Current.Server.MapPath(path);
                if (!Directory.Exists(root))
                {
                    Directory.CreateDirectory(root);
                }
                string linktext = "";
                if (link.Contains("mp.weixin.qq.com"))
                {
                    //WebBrowserCrawler wb = new WebBrowserCrawler();
                    //linktext = wb.GetReult(link);

                    HttpHelper http = new HttpHelper();
                    //获取到网页数据
                    HttpResult result = http.GetHtml(new HttpItem()
                    {
                        URL = link,
                        Referer = link,
                        Allowautoredirect = true
                    });
                    linktext = result.Html.Replace("data-src", "src");
                }
                if (moneytype == 2)
                {
                    allmoney = onemoney * count;
                }
                ActiveInfo info = new ActiveInfo()
                {
                    ID = id,
                    UserID = userid,
                    Name = name,
                    Img = img,
                    Link = link,
                    Mobile = mobile,
                    Context = context,
                    MoneyType = moneytype,
                    Count = count,
                    TakeCount = 0,
                    OneMoney = onemoney,
                    AllMoney = allmoney,
                    TakeMoney = 0,
                    PrizeType = prizetype,
                    Range = range,
                    Province = province,
                    City = city,
                    Area = area,
                    Metre = metre,
                    Lat = lat,
                    Lng = lng,
                    Address = address,
                    State = 1,
                    ClickCount = 0,
                    ShareCount = 0,
                    LinkText = linktext,
                    Degree = degree,
                    Flag = 2,
                    PrizeCount = prizecount,
                    ActiveType = 1,
                    NeedCount = 0,
                    PrizeImg = "",
                    SkipLink = "",
                    GiftCount = 0,
                    WeiXin = "",
                    Keyword = user.Keyword,
                    ShowOver = showover,
                    CreateDate = DateTime.Now
                };
                if (id == 0)
                {
                    ID = ActiveDAL.Save(info);
                    info.AllMoney = info.AllMoney / 100;
                    info.TakeMoney = info.TakeMoney / 100;
                    info.OneMoney = info.OneMoney / 100;
                    DataTable dt = GetTableSchema();
                    float avg = 0;
                    if (count > 0)
                    {
                        avg = allmoney / count;
                    }
                    for (int i = 0; i < count; i++)
                    {
                        float prizemoney = 0;
                        if (info.MoneyType == 1)
                        {
                            if (((int)avg) == 30)
                            {
                                prizemoney = DataConvert.ToFloat("0.3");
                            }
                            else
                            {
                                prizemoney = (info.AllMoney - info.TakeMoney) / (info.Count - info.TakeCount);
                                float rand = 0;
                                //if (prizemoney != 1)
                                //{
                                    rand = DataConvert.ToFloat(new Random(Guid.NewGuid().GetHashCode()).Next((int)(-(prizemoney * 100 - 30)), (int)(prizemoney * 100 - 30 - 1))) / 100;
                                //}
                                prizemoney += rand;
                                //把剩余的红包给最后一个人
                                if (info.Count - info.TakeCount == 1)
                                {
                                    prizemoney = info.AllMoney - info.TakeMoney;
                                }
                                prizemoney = DataConvert.ToFloat(prizemoney.ToString("0.00"));
                            }
                            info.TakeMoney += prizemoney;
                            info.TakeCount++;
                        }
                        else
                        {
                            prizemoney = info.OneMoney;
                        }

                        DataRow dr = dt.NewRow();
                        dr[0] = 0;
                        dr[1] = 0;
                        dr[2] = ID;
                        dr[3] = prizemoney * 100;
                        dr[4] = 0;
                        dt.Rows.Add(dr);
                    }
                    CommonDAL.BatchInsert2(dt);
                    string ewm = path + "/" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
                    wx.SaveFile("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://www.mengshen666.cn/activess.aspx?id=" + DESEncrypt.Encrypt(ID.ToString()), ewm);
                    CommonDAL.UpdateColumnByID(ID, "Ewm", ewm, "[active]");
                    UserDAL.Charge(userid, -allmoney);
                }
                else
                {
                    ID = ActiveDAL.Update(info);

                }
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 获取活动
            hs.Add("Find_Active", () =>
            {
                //参数
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int state = JobRequest.GetRequestInt("state"); //状态
                int type = JobRequest.GetRequestInt("type"); //类型 1.现金红包  2.积攒分享
                int flag = JobRequest.GetRequestInt("flag"); //标记：1.普通活动  2.批量活动
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                int userid = 0;
                List<ActiveInfo> list = ActiveDAL.FindPage(keyword, flag, state, type, begindate, enddate, pageindex, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 获取活动2
            hs.Add("Find_Active2", () =>
            {
                //参数
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int flag = JobRequest.GetRequestInt("flag"); //标记：1.普通活动  2.批量活动
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                int userid = 0;
                List<ActiveInfo> list = ActiveDAL.FindPage2(keyword, flag, begindate, enddate, pageindex, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 获取二维码
            hs.Add("Find_Ewm", () =>
            {
                //参数
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                int activeid = JobRequest.GetRequestInt("activeid"); //活动ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<EwmInfo> list = EwmDAL.FindPage(activeid, pageindex, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 删除活动
            hs.Add("Delete_Active", () =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                ActiveInfo active = ActiveDAL.FindOneByID(DataConvert.ToInt(ids));
                bool ok = CommonDAL.DeleteByIDs(ids, "[active]");
                if (ok)
                {
                    //把活动剩余的钱退还到账户
                    UserDAL.Charge(active.UserID, active.AllMoney * 100 - active.TakeMoney * 100);
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 修改状态
            hs.Add("StateSet_Active", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int state = JobRequest.GetRequestInt("state"); //代理商ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "State", state, "[active]");
                if (ok)
                {
                    if (state == 1)
                    {
                        //DeductionDAL.StartDeduction(id, 1);
                    }
                    if (state == 2)
                    {
                        //PrizeRecordDAL.DecPrizeRecord();
                        //DeductionDAL.PauseDeduction(id, 2);
                    }
                    if (state == 3)
                    {
                        ActiveDAL.Finish(id);
                    }
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 修改发放方式
            hs.Add("IsAutoSet_Active", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int isauto = JobRequest.GetRequestInt("isauto"); //发放方式
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "IsAuto", isauto, "[active]");
                if (ok)
                {
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 批量下载链接
            hs.Add("Download_Link", () =>
            {
                //参数
                int activeid = JobRequest.GetRequestInt("activeid"); //ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<EwmInfo> list = EwmDAL.FindByAcitveID(activeid);
                StringBuilder str = new StringBuilder();
                foreach (EwmInfo info in list)
                {
                    str.Append("http://www.mengshen666.cn/activesss.aspx?id=" + info.IDs + "\r\n");
                }
                string path = Commons.Ewm(str.ToString());
                return Commons.GetSuccess("操作成功", path);

            });
            #endregion
            #region 批量下载二维码
            hs.Add("Download_Ewm", () =>
            {
                //参数
                int activeid = JobRequest.GetRequestInt("activeid"); //ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<EwmInfo> list = EwmDAL.FindByAcitveID(activeid);
                StringBuilder str = new StringBuilder();
                string fold = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string path = HttpContext.Current.Server.MapPath("/upload/ewm/" + fold);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                    int count = 0;
                    WeiXin wx = new WeiXin();
                    foreach (EwmInfo info in list)
                    {
                        count++;
                        string ewm = "/upload/ewm/" + fold + "/" + count + ".jpg";
                        wx.SaveFile("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://www.mengshen666.cn/activesss.aspx?id=" + info.IDs, ewm);
                    }
                }

                SharpZip.CompressFile(path, HttpContext.Current.Server.MapPath("/upload/ewm/" + fold + ".rar"));
                return Commons.GetSuccess("操作成功", "/upload/ewm/" + fold + ".rar");

            });
            #endregion
            #region 修改剩余红包数量
            hs.Add("Update_TakeCount", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int increasecount = JobRequest.GetRequestInt("increasecount"); //扣除红包数量
                decimal hour = JobRequest.GetRequestDecimal("hour", 0); //持续小时数
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                ActiveInfo active = ActiveDAL.FindOneByID(JobRequest.GetRequestInt("id"));
                int count = (active.Count - active.TakeCount - increasecount);
                if (count < 0)
                {
                    return Commons.GetFaild("扣除数量不能大于剩余数量");
                }
                if (count == 0)
                {
                    CommonDAL.UpdateColumnByID(id, "State", 3, "[active]");
                }
                /*if (hour > 0)
                {
                    if (DeductionDAL.HasRun(id))
                    {
                        return Commons.GetFaild("有正在扣量的任务还没有执行完成");
                    }
                    DeductionInfo info = new DeductionInfo()
                    {
                        ActiveID = id,
                        AllCount = increasecount,
                        ActualCount = 0,
                        State = 1,
                        Hour = hour,
                        LastEditTime = DateTime.Now,
                        CreateDate = DateTime.Now
                    };

                    int ID = DeductionDAL.Save(info);

                    if (ID > 0)
                    {
                        return Commons.GetSuccess("操作成功", "");
                    }
                    else
                    {
                        return Commons.GetFaild("操作失败");
                    }
                }
                else
                {
                */
                    bool ok = false;
                    double takemoney = 0;
                    for (int i = 0; i < increasecount; i++)
                    {
                        double prizemoney = 0;
                        prizemoney = PrizeRecordDAL.RedPacket(id, i + 1);

                        prizemoney = prizemoney * 100;
                        PrizeRecordInfo info = new PrizeRecordInfo()
                        {
                            UserID = i + 1,
                            SID = id,
                            Money = prizemoney,
                            PrizeType = active.PrizeType,
                            Province = "",
                            City = "",
                            Area = "",
                            Lat = 0,
                            Lng = 0,
                            Address = "",
                            CreateID = active.UserID,
                            CreateDate = DateTime.Now
                        };
                        PrizeRecordDAL.Save(info);
                        takemoney += prizemoney;
                    }
                    ok =  ActiveDAL.UpdateTakeMoney(id, increasecount, takemoney);
                    if (ok)
                    {
                        VisitRecordInfo visit = new VisitRecordInfo()
                        {
                            UserID = 0,
                            SID = active.ID,
                            Province = "",
                            City = "",
                            Area = "",
                            Lat = 0,
                            Lng = 0,
                            Address = "",
                            CreateID = active.UserID,
                            FCount = increasecount,
                            Flag = 2,
                            CreateDate = DateTime.Now
                        };
                        ActiveDAL.IncClickCount(id, increasecount);
                        ActiveDAL.IncShareCount(id, increasecount);
                        VisitRecordDAL.Save(visit);
                        return Commons.GetSuccess("操作成功", "");
                    }
                    else
                    {
                        return Commons.GetFaild("操作失败");
                    }
                //}
            });
            #endregion
            #region 修改点击量
            hs.Add("Update_ClickCount", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int hourcount = JobRequest.GetRequestInt("hourcount"); //每小时点击数量
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                ActiveInfo active = ActiveDAL.FindOneByID(JobRequest.GetRequestInt("id"));

                if (ReduceDAL.HasRun(id))
                {
                    return Commons.GetFaild("有正在扣量的任务还没有执行完成");
                }
                ReduceInfo info = new ReduceInfo()
                {
                    ActiveID = id,
                    State = 1,
                    HourCount = hourcount,
                    LastEditTime = DateTime.Now,
                    CreateDate = DateTime.Now
                };

                int ID = ReduceDAL.Save(info);
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 设置关键词
            hs.Add("Set_ActiveKeyword", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                string keyword = JobRequest.GetRequestString("keyword"); //关键词
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "Keyword", keyword, "[active]");
                if (ok)
                {
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 一键兼容
            hs.Add("Down_Img", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                ActiveInfo active = ActiveDAL.FindOneByID(id);

                HttpHelper http = new HttpHelper();
                //获取到网页数据
                HttpResult result = http.GetHtml(new HttpItem()
                {
                    URL = active.Link,
                    Referer = active.Link,
                    Allowautoredirect = true
                });
                string linktext = result.Html.Replace("data-src", "src");
                linktext = Commons.NoScript(linktext).Replace("<span class=\"article-tag-card__title\">收录于话题</span>", "");
                linktext = Commons.DownImg(linktext);
                bool ok = ActiveDAL.UpdateLinkText(id, linktext);
                if (ok)
                {
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 更新二维码
            hs.Add("Update_Ewm", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                try
                {
                    ActiveInfo info = ActiveDAL.FindOneByID(id);
                    WeiXin wx = new WeiXin();
                    info.Ewm = info.Ewm.Replace(Commons.domain, "");
                    string path = HttpContext.Current.Server.MapPath(info.Ewm.Substring(0, info.Ewm.LastIndexOf('/')));
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    wx.SaveFile("http://api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://www.mengshen666.cn/activesss.aspx?id=" + DESEncrypt.Encrypt(id.ToString()), info.Ewm);
                    return Commons.GetSuccess("操作成功", "");
                }
                catch (Exception ex)
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 批量更新二维码
            hs.Add("Update_BatchEwm", () =>
            {
                //参数
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int state = JobRequest.GetRequestInt("state"); //状态
                int type = JobRequest.GetRequestInt("type"); //类型 1.现金红包  2.积攒分享
                int flag = JobRequest.GetRequestInt("flag"); //标记：1.普通活动  2.批量活动
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                try
                {
                    List<ActiveInfo> list = ActiveDAL.FindBatchList(keyword, flag, state, type, begindate, enddate);
                    WeiXin wx = new WeiXin();
                    foreach (ActiveInfo info in list)
                    {
                        info.Ewm = info.Ewm.Replace(Commons.domain, "");
                        string path = HttpContext.Current.Server.MapPath(info.Ewm.Substring(0, info.Ewm.LastIndexOf('/')));
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        wx.SaveFile("http://api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://www.mengshen666.cn/activesss.aspx?id=" + DESEncrypt.Encrypt(info.ID.ToString()), info.Ewm);
                    }
                    return Commons.GetSuccess("操作成功", "");
                }
                catch (Exception ex)
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            

            /*扣量*/
            #region 获取点击量扣量
            hs.Add("Find_Reduce", () =>
            {
                //参数
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int userid = JobRequest.GetRequestInt("userid");//代理商id
                int state = JobRequest.GetRequestInt("state"); //状态
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<ReduceInfo> list = ReduceDAL.FindPage(keyword, state, begindate, enddate, pageindex, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 修改状态
            hs.Add("StateSet_Reduce", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int state = JobRequest.GetRequestInt("state"); //状态
                int hourcount = JobRequest.GetRequestInt("hourcount"); //数量
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                ReduceInfo info = new ReduceInfo()
                {
                    ID=id,
                    HourCount=hourcount,
                    State=state,
                };
                int ID= ReduceDAL.Update(info);
                if (ID>0)
                {
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 删除扣量
            hs.Add("Delete_Reduce", () =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.DeleteByIDs(ids, "[reduce]");
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion

            /*红包记录*/
            #region 红包记录
            hs.Add("Find_PrizeRecord", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //活动ID
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<PrizeRecordInfo> list = PrizeRecordDAL.FindPage(keyword, id, pageindex, 20, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 发放红包
            hs.Add("Set_IsAuto", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                PrizeRecordInfo info = PrizeRecordDAL.FindOneByID(id);
                UserInfo user = UserDAL.FindOneByID(info.UserID);
                ActiveInfo active = ActiveDAL.FindOneByID(info.SID);
                UserInfo uu = UserDAL.FindOneByID(active.UserID);
                string result = WxPayAPI.WxPayApi.CompanyPay(user.OpenID, (int)(info.Money * 100), uu.Business);
                if (result.Contains("支付失败"))
                {
                    return Commons.GetFaild("支付失败，该用户未实名认证或余额不足");
                }
                else
                {
                    CommonDAL.UpdateColumnByID(id, "isauto", 1, "prize_record");
                    return Commons.GetSuccess("操作成功", "");
                }

            });
            #endregion

            /*充值记录*/
            #region 获取充值记录
            hs.Add("Find_Charge", () =>
            {
                //参数
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int userid = JobRequest.GetRequestInt("userid");//代理商id
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<ChargeInfo> list = ChargeDAL.FindPage(userid, keyword, pageindex, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion

            /*合作申请*/
            #region 获取合作申请
            hs.Add("Find_Apply", () =>
            {
                //参数
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int userid = JobRequest.GetRequestInt("userid");//代理商id
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<ApplyInfo> list = ApplyDAL.FindPage(keyword, begindate, enddate, pageindex, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 分配合作申请
            hs.Add("AdminSet_Apply", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int adminid = JobRequest.GetRequestInt("adminid"); //代理商ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "[AdminID]", adminid, "[Apply]");
                if (ok)
                {
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion

            /*客服*/
            #region 获取客服
            hs.Add("Find_Kf", () =>
            {
                //参数
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<KfInfo> list = KfDAL.FindPage(keyword, pageindex, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 保存客服
            hs.Add("Save_Kf", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                string fname = JobRequest.GetRequestString("fname"); //客服姓名
                string mobile = JobRequest.GetRequestString("mobile"); //客服电话
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                KfInfo info = new KfInfo()
                {
                    ID = id,
                    Mobile = mobile,
                    FName = fname
                };
                if (id == 0)
                {
                    ID = KfDAL.Save(info);
                }
                else
                {

                    ID = KfDAL.Update(info);
                }
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存成功");
                }
            });
            #endregion
            #region 删除客服
            hs.Add("Delete_Kf", () =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.DeleteByIDs(ids, "[kf]");
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion

            /*代理商信息*/
            #region 获取代理商信息
            hs.Add("Find_Agent", () =>
            {
                //参数
                int page = JobRequest.GetRequestInt("pageindex"); //第几页
                int limit = JobRequest.GetRequestInt("limit"); //每页条数
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数-AgentInfo
                int count = 0;
                List<AgentInfo> list = AgentDAL.FindPage(keyword, page, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 保存代理商信息
            hs.Add("Save_Agent", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //代理商信息
                string agentname = JobRequest.GetRequestString("agentname");//代理商姓名
                int adminid = JobRequest.GetRequestInt("adminid"); //代理商id
                string mobile = JobRequest.GetRequestString("mobile"); //电话
                string wechat = JobRequest.GetRequestString("wechat"); //微信
                string qrcode = JobRequest.GetRequestString("img"); //二维码
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                AgentInfo info = new AgentInfo()
                {
                    ID = id,
                    AgentName = agentname,
                    AdminID = adminid,
                    Mobile = mobile,
                    WeChat = wechat,
                    QRCode = qrcode,
                    CreateDate = DateTime.Now
                };
                if (id == 0)
                {
                    ID = AgentDAL.Save(info);
                }
                else
                {
                    ID = AgentDAL.Update(info);
                }
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 删除代理商信息
            hs.Add("Delete_Agent", () =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.DeleteByIDs(ids, "[agent]");
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion

            /*修改配置信息*/
            #region 修改配置信息
            hs.Add("Update_Config", () =>
            {
                //参数
                float membermoney = JobRequest.GetRequestFloat("membermoney") * 100; //会员费用
                float backmoney = JobRequest.GetRequestFloat("backmoney") * 100; //返现金额
                float shouxufei = JobRequest.GetRequestFloat("shouxufei") * 100; //充值手续费
                float shouxufei2 = JobRequest.GetRequestFloat("shouxufei2") * 100; //非会员充值手续费
                int sendcount = JobRequest.GetRequestInt("sendcount") * 100; //非会员起发红包数量
                float maxmoney = JobRequest.GetRequestFloat("maxmoney") * 100; //最大金额
                int maxredcount = JobRequest.GetRequestInt("maxredcount"); //每分钟领取红包数量
                //参数
                ConfigInfo info = new ConfigInfo()
                {
                    ID = 1,
                    MemberMoney = membermoney,
                    BackMoney = backmoney,
                    ShouXuFei = shouxufei,
                    ShouXuFei2 = shouxufei2,
                    SendCount = sendcount,
                    MaxMoney = maxmoney,
                    MaxRedCount = maxredcount
                };
                int ID = ConfigDAL.Edit(info);
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", "");
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion

        }
        static bool CheckSign()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                string action = JobRequest.GetRequestString("act");
                string sign = JobRequest.GetRequestString("sign");
                sb.AppendFormat("account={0}&act={1}&key={2}", "Common_CLIENT", action, "365E6B86CE4E43A552G563078D5ADEFD");
                string signstr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sb.ToString(), "MD5").ToLower();
                return sign == signstr;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static void DeleteFolder(string dir)
        {
            foreach (string d in Directory.GetFileSystemEntries(dir))
            {
                if (File.Exists(d))
                {
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    File.Delete(d);//直接删除其中的文件   
                }
                else
                    DeleteFolder(d);//递归删除子文件夹   
            }
            Directory.Delete(dir);//删除已空文件夹   
        }
        public static DataTable GetTableSchema()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] {
            new DataColumn("ID",typeof(int)),  
            new DataColumn("UserID",typeof(int)),  
            new DataColumn("ActiveID",typeof(int)),  
            new DataColumn("Money",typeof(double)),
            new DataColumn("IsTake",typeof(int))
            });
            return dt;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}